package co.edu.unal.satacredito.rest.resources.general;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import co.edu.unal.satacredito.rest.activities.general.ValidateGenericTransactionActivity;
import co.edu.unal.satacredito.rest.domain.general.GenericTransaction;
import co.edu.unal.satacredito.rest.exceptions.visa.NoSuchTransactionException;
import co.edu.unal.satacredito.rest.representations.Representation;
import co.edu.unal.satacredito.rest.representations.RestUri;
import co.edu.unal.satacredito.rest.representations.general.GenericTransactionRepresentation;

@Path("/validation/generic/")
public class GenericTransactionResource {

	public GenericTransactionResource() {
	}

	private @Context
	UriInfo uriInfo;

	@POST
	@Consumes(Representation.SATACREDITO_MEDIA_TYPE)
	@Produces(Representation.SATACREDITO_MEDIA_TYPE)
	public Response validateTransaction(
			GenericTransaction genericTransaction) {
		try {
			GenericTransactionRepresentation responseRepresentation = new ValidateGenericTransactionActivity()
					.validate(genericTransaction,
							new RestUri(uriInfo.getRequestUri()));
			return Response.ok().entity(responseRepresentation).build();
		} catch (NoSuchTransactionException nsoe) {
			nsoe.printStackTrace();
			return Response.status(Status.NOT_FOUND).build();
		} catch (Exception ex) {
			ex.printStackTrace();
			return Response.serverError().build();
		}
	}
}
